package com.Hsu.trans.controller;

import com.Hsu.trans.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author Hsu琛君珩
 * @ClassName:TransController
 * @date 2024-01-22
 * @apiNote
 * @Version: v1.0
 */

@Slf4j
@RequestMapping("trans")
@RestController
public class TransController {
    @Autowired
    private UserService userService;

    /**
     * 正常操作，没有抛出异常
     */
    @Transactional
    @RequestMapping("/registry")
    public String registry(String userName, String password) {
        Integer result = userService.insertUser(userName, password);
        log.info("用户插入成功，result:" + result);
        return "注册成功";
    }

    /**
     * 抛出异常
     * 程序报错，没有捕获
     */
    @Transactional
    @RequestMapping("/registry2")
    public String registry2(String userName, String password) {
        Integer result = userService.insertUser(userName, password);
        log.info("用户插入成功，result:" + result);
        int a = 10 / 0;
        return "注册成功";
    }

    /**
     * 程序报错，但捕获了
     */
    @Transactional
    @RequestMapping("/registry3")
    public String registry3(String userName, String password) {
        Integer result = userService.insertUser(userName, password);
        log.info("用户插入成功，result:" + result);
        try {
            int a = 10 / 0;
        } catch (Exception e) {
            log.error("程序出错");
        }
        return "注册成功";
    }

    /**
     * 程序报错，但捕获了，又抛出了
     * 事务依然回滚了
     */
    @Transactional
    @RequestMapping("/registry4")
    public String registry4(String userName, String password) {
        Integer result = userService.insertUser(userName, password);
        log.info("用户插入成功，result:" + result);
        try {
            int a = 10 / 0;
        } catch (Exception e) {
            throw e;
        }
        return "注册成功";
    }

    /**
     * 程序报错，手动回滚
     */
    @Transactional
    @RequestMapping("/registry5")
    public String registry5(String userName, String password) {
        Integer result = userService.insertUser(userName, password);
        log.info("用户插入成功，result:" + result);
        try {
            int a = 10 / 0;
        } catch (Exception e) {
            log.error("程序出错");
            //当前事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return "注册成功";
    }

    /**
     * 程序报错，但捕获了
     * 又抛出了一个别的异常
     */
    @Transactional
    @RequestMapping("/registry6")
    public String registry6(String userName, String password) throws IOException {
        Integer result = userService.insertUser(userName, password);
        log.info("用户插入成功，result:" + result);
        try {
            int a = 10 / 0;
        } catch (Exception e) {
            log.error("程序出错");
            throw new IOException();
        }
        return "注册成功";
    }

    /**
     * 程序报错，但捕获了
     * 又抛出了一个别的异常
     */
    @Transactional(rollbackFor = {Exception.class, Error.class}, isolation = Isolation.DEFAULT)
    @RequestMapping("/registry7")
    public String registry7(String userName, String password) throws IOException {
        Integer result = userService.insertUser(userName, password);
        log.info("用户插入成功，result:" + result);
        try {
            int a = 10 / 0;
        } catch (Exception e) {
            log.error("程序出错");
            throw new IOException();
        }
        return "注册成功";
    }
}